const api = getApp().globalData.api
Page({
    data: {
        result: {},
        show: false,
        inRecord: [],  //入库记录
        actions: [
            {
                name: '删除',
                subname: '该操作是不可逆的，是否确认？',
            },
        ],
    },
    detail: async function (id, barcode) {
        let that = this
        let res = await api.curlPost('?cla=inStock&fun=detail', {id: id})
        if (res.code === 1) {
          that.setData({result: res.data.result, inRecord: []})
          // 如果有物料码则跳转获取物料码数据
          if(barcode){
            that.caseInCheck({barcode: barcode})
          }
        } else {
            api.showToast(res.warn)
            wx.navigateBack({delta: 1})
        }
    },
    onLoad: async function (options) {
        const id = options.id ? options.id : ''
        const barcode = options.barcode ? options.barcode : ''
        if (!id) {
            api.showToast('参数错误')
            wx.navigateTo({
                url: 'index'
            })
        } else {
            this.detail(id, barcode)
        }
    },
    // 刷新入库数据
    onShow: async function () {
      const that = this
      const inData = wx.getStorageSync('inData')
      if(inData.id){
        let currentRecord = that.data.inRecord
        if(inData.key){
          currentRecord[inData.key] = inData
        }else{
          currentRecord.push(inData)
        }
        // 当前总入库数量
        let inNum = 0
        for(var index in currentRecord){
          if (currentRecord[index].id === inData.id) {
            inNum += currentRecord[index].inNum
          }
        }
        currentRecord[index].num = inNum
        // let currentLog = that.data.result.inRecord
        // currentLog.push(inData)
        // 遍历数组找到对应的用户  
        let detail = this.data.result.detail.map(mx => {  
          if (mx.id === inData.id) {
            // 如果找到物料则更新数量
            mx.num = Number(inNum);  
          }
          return mx;  
        });  
        let detailtodo = 'result.detail';
        // let inLogtodo = 'result.inRecord';
        that.setData({
            [detailtodo]: detail,
            //[inLogtodo]: currentLog,
            inRecord: currentRecord
        })
        wx.removeStorageSync('inData')
      }
    },
    goto: function (obj) {
        return api.goto(obj)
    },
    // 提交
    onDone: async function (obj) {
      let that = this
      let data = obj.currentTarget.dataset
      if (!data.id) {
          api.showToast('参数错误')
      } else {
          let res = await api.curlPost(
            '?cla=inStock&fun=done',
              {
                id: data.id,
                inRecord: JSON.stringify(that.data.inRecord),
              }
            )
          if (res.code !== 1) {
              api.showToast(res.warn)
          } else {
              api.showToast('入库完成')
              wx.removeStorageSync('inStockData')
              setTimeout(function () {
                that.detail(data.id, '')
              }, 1000)
          }
      }
    },
    onPass() {
        api.showToast('执行通过操作')
    },
    onScan: async function (e) {
      let that = this
      wx.scanCode({
          onlyFromCamera: true,
          success(res) {
              if (res.errMsg === "scanCode:ok") {
                  const string = res.result
                  let array = api.parseStr(string)
                  // 扫描物料码
                  if(string.indexOf("=") === -1){
                    array = {barcode: string}
                  }
                  // 通过箱码，匹配入库数据
                  that.caseInCheck(array)
              }
          }
      })
  },
  caseInCheck: async function (array) {
    let that = this
    array['id'] = that.data.result.info.id
    let res = await api.curlPost('?cla=inStock&fun=caseInCheck', array)
      if (res.code !== 1) {
          api.showToast(res.warn)
      } else {
        // 跳转确认入库
        setTimeout(function () {
          wx.navigateTo({
              url: 'in?data=' + JSON.stringify(res.data)
          })
        }, 1000)
      }
  },
  // 删除
  delMaterial(obj) {
    let index = obj.currentTarget.dataset.index,
        id = obj.currentTarget.dataset.id
    let inStockData = this.data.inRecord
    inStockData.splice(index, 1);
    // 当前总入库数量
    let inNum = 0
    for(var key in inStockData){
      if (inStockData[key].id === id) {
        inNum += inStockData[key].inNum
      }
    }
    // 遍历数组找到对应的用户  
    let detail = this.data.result.detail.map(mx => {  
      if (mx.id === id) {
        // 如果找到物料则更新数量
        mx.num = Number(inNum);  
      }
      return mx;  
    });  
    let detailtodo = 'result.detail';
    // let inLogtodo = 'result.inRecord';
    this.setData({
        [detailtodo]: detail,
        inRecord: inStockData
    })
  }
});